package com.handpay.nocardpay.baselibrary.guide.view.fragment;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.account.view.activity.LoginActivity;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.utils.NoCardPayConstant;

/**
 * Created by sxshi on 2017/8/29.
 */

public class AdvertisingFragment extends BaseFragment {
    private WebView mWebView;
    private TextView tv_jump;
    private ImageView iv_loading;
    private CountDownTimer mCountDownTimer;

    private String advertUrl;

    public static AdvertisingFragment newInstance() {

        Bundle args = new Bundle();

        AdvertisingFragment fragment = new AdvertisingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_advertising;
    }

    @Override
    public void initView(View view) {
        mWebView = (WebView) view.findViewById(R.id.wv_content);
        tv_jump = (TextView) view.findViewById(R.id.tv_jump);
        iv_loading = (ImageView) view.findViewById(R.id.iv_loading);
    }

    @Override
    public void initListener() {
        tv_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goLogin();
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                HPLog.d(HPLog.SSX_TAG, "onReceivedSslError"+error.toString());
//                super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                HPLog.d(HPLog.SSX_TAG, "shouldOverrideUrlLoading:url6.0:"+request.getUrl());
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                HPLog.d(HPLog.SSX_TAG, "shouldOverrideUrlLoading4.4:url:"+url);
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view,url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                HPLog.d(HPLog.SSX_TAG, "onPageFinished:url:"+url);
                //网页加载完成开始计时，显示网页
                iv_loading.setVisibility(View.GONE);
                mCountDownTimer.start();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                HPLog.e(HPLog.SSX_TAG, errorCode + "-" + description);
                HPLog.e(HPLog.SSX_TAG, failingUrl);
                HPLog.d(HPLog.SSX_TAG, "onReceivedError4.4");
                goLogin();
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                HPLog.e(HPLog.SSX_TAG, error.getErrorCode() + "-" + error.getDescription());
                HPLog.e(HPLog.SSX_TAG, request.getUrl().getPath() + "-" + request.getUrl().getHost());
                HPLog.d(HPLog.SSX_TAG, "onReceivedError6.0");
                goLogin();
            }
        });
    }

    /**
     * 登录页
     */
    private void goLogin() {
        Intent intent = new Intent(_mActivity, LoginActivity.class);
        _mActivity.startActivity(intent);
        _mActivity.finish();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        advertUrl = NoCardPayConfig.ADVERTURL;
        if (!TextUtils.isEmpty(advertUrl)) {
            HPLog.d(HPLog.SSX_TAG,"url:"+advertUrl);
            mWebView.loadUrl(advertUrl);
        }
        mCountDownTimer = new CountDownTimer(NoCardPayConstant.MILLISADV, NoCardPayConstant.COUNTDOWNINTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {
                tv_jump.setText((millisUntilFinished / 1000) + "  跳过");
            }

            @Override
            public void onFinish() {
                goLogin();
            }
        };
    }
}
